﻿using System;
using System.Web.Mvc;
using BestEasyCMS.Framework.Authentication;
using BestEasyCMS.Framework.CookieStorage;
using BestEasyCMS.Framework.Extension;
using BestEasyCMS.Service.Interfaces.Security;
using BestEasyCMS.Service.Messages.Security.Request;
using BestEasyCMS.Web.MvcUI.Areas.BestEasyAdmin.Helper;
using BestEasyCMS.Web.MvcUI.Areas.BestEasyAdmin.Models;
using BestEasyCMS.Web.MvcUI.Helper;

namespace BestEasyCMS.Web.MvcUI.Areas.BestEasyAdmin.Controllers.Account
{
    public class AccountController : BaseController
    {
        private readonly IUserService _userService;
        private readonly IFormsAuthentication _authentication;
        private readonly ICookieStorageService _cookieStorageService;

        public AccountController(IUserService userService, IFormsAuthentication authentication, ICookieStorageService cookieStorageService)
        {
            _userService = userService;
            _authentication = authentication;
            _cookieStorageService = cookieStorageService;
        }

        //public ActionResult Index()
        //{
        //    return View();
        //}

        [AllowAnonymous]
        public ActionResult Login()
        {
            ViewBag.LoginName = _cookieStorageService.GetCookieValue(CookieKeyHelper.UserCookieName,
                CookieKeyHelper.CurrentUserName);
            return View();
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public JsonResult Login(LoginInfo info)
        {
            var request = new LoginRequest(info.LoginName, info.Password.ToMd5());
            var response = _userService.Login(request);
            if (response.IsSuccess)
            {
                var view = response.View;
                //保存登录状态
                _authentication.SetAuthenticationToken(view.LoginName);
                _cookieStorageService.AddCookie(CookieKeyHelper.UserCookieName, CookieKeyHelper.CurrentUId, view.Id.ToStr(), DateTime.Now.AddMonths(2));
                //保存信息
                AccountHelper.SetAccount(new CurrentAccount { UserId = view.Id, RoleId = view.RoleId, LoginName = view.LoginName, RoleName = view.RoleView.RoleName });
                //保存登录名
                if (info.RememberMe == "on")
                {
                    _cookieStorageService.SetCookie(CookieKeyHelper.UserCookieName, CookieKeyHelper.CurrentUserName, view.LoginName.ToStr(), DateTime.Now.AddMonths(2));
                }
                else
                {
                    _cookieStorageService.RemoveCookie(CookieKeyHelper.UserCookieName, CookieKeyHelper.CurrentUserName);
                }
            }
            return Json(DataHelper.GetResult(response.IsSuccess, response.Message, response.View));
        }
        //退出登录
        public ActionResult SignOut()
        {
            _authentication.SignOut();
            _cookieStorageService.RemoveCookie(CookieKeyHelper.UserCookieName, CookieKeyHelper.CurrentUId);
            return RedirectToAction("Login");
        }
    }
}
